package models

import play.api.db.slick.Config.driver.simple._
import java.util.Date
import scala.slick.lifted.MappedTypeMapper


case class BlogAuthor(id: Option[Long] = None, name: String, email: String)

trait BlogAuthorComponent {

}

object BlogAuthors extends Table[BlogAuthor]("Member") {
  def id = column[Long]("id", O.PrimaryKey, O.AutoInc)

  def name = column[String]("name", O.NotNull)

  def email = column[String]("email", O.NotNull, O.DBType("VARCHAR(120)"))

  def * = id.? ~ name ~ email <>(BlogAuthor, BlogAuthor.unapply _)
}

case class Blog(id: Option[Long] = None, title: String, body: String, authorId: Option[Long])

object Blogs extends Table[Blog]("Blog") {
  /*
  implicit val javaUtilDateTypeMapper = MappedTypeMapper.base[java.util.Date, java.sql.Date](
    x => new java.sql.Date(x.getTime),
    x => new java.util.Date(x.getTime)
  )
  */
  implicit val javaUtilTimestampMapper = MappedTypeMapper.base[java.util.Date, java.sql.Timestamp](
    x => new java.sql.Timestamp(x.getTime),
    x => new java.util.Date(x.getTime)
  )

  def id = column[Long]("Id", O.PrimaryKey, O.AutoInc)

  def title = column[String]("Title", O.NotNull)

  def body = column[String]("Body", O.NotNull)

  def authorId = column[Long]("AuthorId", O.Nullable)

  def author = foreignKey("FK_author", authorId, BlogAuthors)(_.id)

  def author2 = BlogAuthors.where(_.id == authorId)

  def createTime = column[Date]("CreateTime")

  def * = id.? ~ title ~ body ~ authorId.? <>(Blog, Blog.unapply _)

}
